//
//  MyViewController.m
//  B
//
//  Created by 王展 on 15/11/5.
//  Copyright © 2015年 wangzhan. All rights reserved.
//

#import "MyViewController.h"
#import "CollectViewController.h"
@interface MyViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)UITableView *myTableView;
@property(nonatomic,retain)UIImageView *imageViews;

@property (nonatomic, retain)UIAlertView *successClear;
@end

@implementation MyViewController
-(void)viewWillAppear:(BOOL)animated {
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    [super viewWillAppear:animated];
   
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor =[UIColor blueColor];
    self.navigationItem.title = @"我";
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 375*XWIDTH, (603)*XHIGHT) style:UITableViewStylePlain];
    [self.view addSubview:self.myTableView];
    
    
    //设置tableview的contentView距离上边界200
    //相对于0点,已经向下偏移了-200
    self.myTableView.contentInset = UIEdgeInsetsMake(200.0*XWIDTH, 0, 0, 0);
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    
    
    //相对于0点,图片坐标应该是(0,-200)
    self.imageViews = [[UIImageView alloc] initWithFrame:CGRectMake(0, -200.0*XHIGHT, 375*XWIDTH, 200.0*XHIGHT)];
    self.imageViews.image = [UIImage imageNamed:@"icon.jpg"];
    //设置imageView高度改变时宽度也跟着改变
    self.imageViews.contentMode = UIViewContentModeScaleAspectFill;
    self.imageViews.backgroundColor = [UIColor blueColor];
    [self.myTableView insertSubview:self.imageViews atIndex:0];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
    
    
    
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 4;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50*XHIGHT;
    
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellIndentifier = @"reuse";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
    }
    if (indexPath.row == 0) {
        //cell.textLabel.frame = CGRectMake(55, 5, 100, 30);
        cell.textLabel.font = [UIFont systemFontOfSize:16 ];
        cell.textLabel.text = @"\t\t夜间模式";
        UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"iconfont-yejianzhuanhuan.png"]];
        image.frame = CGRectMake(5*XWIDTH, 12*XHIGHT, 20*XWIDTH, 25*XHIGHT);
        [cell addSubview:image];
        //       cell.image = [UIImage imageNamed:@"iconfont-yejianzhuanhuan.png"];
    }
    if (indexPath.row == 1) {
        cell.textLabel.text = @"\t\t清除缓存";
        cell.textLabel.font = [UIFont systemFontOfSize:16 ];
        cell.textLabel.backgroundColor = [UIColor blueColor];
        UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"iconfont-qingchuyuhuancun.png"]];
        image.frame = CGRectMake(5*XWIDTH, 12*XHIGHT, 20*XWIDTH, 25*XHIGHT);
        [cell addSubview:image];
    }
    if (indexPath.row == 2) {
        cell.textLabel.text = @"\t\t我的收藏";
        cell.textLabel.font = [UIFont systemFontOfSize:16 ];
        UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"iconfont-wodeshoucang.png"]];
        image.frame = CGRectMake(5*XWIDTH, 12*XHIGHT, 20*XWIDTH, 25*XHIGHT);
        [cell addSubview:image];
    }
    
    
    return cell;
    
    
}
//缓存文件个数
- (NSInteger)numberOfFile{
    NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)objectAtIndex:0];
    NSArray *files = [[NSFileManager defaultManager]subpathsAtPath:cachPath];
    return [files count];
}

//清除缓存文件
- (void)clearMyCacheAction{
    
    
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
        NSArray *files = [[NSFileManager defaultManager]subpathsAtPath:cachPath];
        for (NSString *p in files) {
            NSError *error;
            NSString *path = [cachPath stringByAppendingPathComponent:p];
            if ([[NSFileManager defaultManager]fileExistsAtPath:path]) {
                [[NSFileManager defaultManager]removeItemAtPath:path error:&error];
            }
        }
        [self performSelectorOnMainThread:@selector(clearSuccess) withObject:nil waitUntilDone:YES];
    });
}
//点击清除 清除缓存
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        [self clearMyCacheAction];
        [[SDImageCache sharedImageCache]cleanDisk];
    }
}
-(void)Action{
    
    self.successClear = [[UIAlertView alloc] initWithTitle:nil message:@"清除成功" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
    [NSTimer scheduledTimerWithTimeInterval:1.5f target:self selector:@selector(dismissP1:) userInfo:nil repeats:NO];
    
    [self.successClear show];
    
}
- (void)dismissP1:(NSTimer *)timer
{
    [self.successClear dismissWithClickedButtonIndex:0 animated:NO];
}





- (void)clearSuccess
{
    
    self.successClear = [[UIAlertView alloc] initWithTitle:nil message:@"清除成功" delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
    [NSTimer scheduledTimerWithTimeInterval:1.5f target:self selector:@selector(dismissP:) userInfo:nil repeats:NO];
    
    [self.successClear show];
    
}
- (void)dismissP:(NSTimer *)timer
{
    [self.successClear dismissWithClickedButtonIndex:0 animated:NO];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    if (indexPath.row == 1) {
        
        NSUInteger size = [[SDImageCache sharedImageCache]getSize];
        CGFloat cache = size / 1024. / 1024.;
        NSString *strCa = [NSString stringWithFormat:@"%.2fMB", cache];
        
        
        NSString *str = [NSString stringWithFormat:@"一共有%ld个文件可以清除, 约%@", (long)[self numberOfFile], strCa];
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"清除缓存" message:str delegate:self cancelButtonTitle:@"返回" otherButtonTitles:@"清除", nil];
        [alert show];
       
        
        
    }if (indexPath.row==2) {
        
    
    CollectViewController *collect = [[CollectViewController alloc]init];
    [self.navigationController pushViewController:collect animated:NO];
    }if (indexPath.row ==0) {
    
        NSString *str = [[NSUserDefaults standardUserDefaults]objectForKey:@"night"];
        if ([str isEqualToString:@"YES"]) {
            [[NSUserDefaults standardUserDefaults]setObject:@"NO" forKey:@"night" ];
            [[NSNotificationCenter defaultCenter]postNotificationName:@"changeColor" object:@"NO"];//postNotificationName:通知的名字
        }
        else{
            
            [[NSNotificationCenter defaultCenter]postNotificationName:@"changeColor" object:@"YES"];
        }
    }
}


//    ShopDeatailViewController *deatailVC= [[ShopDeatailViewController alloc]init];
//
//    shopModel *shopM = [self.allArray objectAtIndex:indexPath.row];
//
//    deatailVC.shopM =shopM;
//
//    [self. navigationController pushViewController:deatailVC animated:YES];


-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    //刚开始y的偏移量初始值就是-264
    NSLog(@"y1 === %f",scrollView.contentOffset.y);
    CGFloat y = scrollView.contentOffset.y ;//加上导航栏高度,第一次是-200
    NSLog(@"y2 === %f",y);
    
    if (y < -200*XHIGHT) {
        CGRect frame = self.imageViews.frame;
        frame.origin.y = y;//imageView的frame是不断往上偏移
        frame.size.height =  -y;//tablview向下偏移了多少,高度就增加多少
        self.imageViews.frame = frame;
    }
    
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
